'use strict';
app.controller('agentEditCtrl', ['$scope', '$modalInstance', '$state', 'toaster', 'agentService', 'id',
    function($scope, $modalInstance, $state, toaster, agentService, id) {

        $scope.agent = {
            id: id,
            name: '',
            idCard: '',
            gender: 0,
            mobilePhone: '',
            address: '',
            email: ''
        };

        // 初始化待修改的数据
        $scope.init = function () {
            agentService.find($scope, id).then(function(result){
                switch (result.status) {
                    case 200:
                        var rData = result.data.data;
                        $scope.agent = {
                            id: rData.id,
                            name: rData.name,
                            idCard: rData.idCard,
                            gender: rData.gender,
                            mobilePhone: rData.mobilePhone,
                            address: rData.address,
                            email: rData.email
                        };
                        agentService.selectedAgentArea($scope, rData.agentAreaId).then(function (result2) {
                            switch (result2.status) {
                                case 200:
                                    var rData2 = result2.data.data;
                                    $scope.uiSelectList = [];
                                    $scope.uiSelectList[0] = rData2;
                                    $scope.agent.selected = $scope.uiSelectList[0];
                                    break;
                                default :
                                    toaster.pop('error', "初始失败", "初始化代理商代理区域下拉列表失败");
                                    break;
                            }
                        });
                        break;
                    default :
                        toaster.pop('error', "初始失败", "初始化代理商信息失败");
                        break;
                }
            });
        }
        $scope.init();

        // 关键字搜索下拉代理区域列表
        $scope.uiSelectList = [];
        $scope.refreshSelectList = function (keyword) {
            if(keyword.length <= 0) {
                $scope.uiSelectList = [];
                $scope.uiSelectList[0] = $scope.agent.selected;
                return;
            }
            agentService.initAgentAreaSelect($scope, keyword).then(function(result){
                switch (result.status) {
                    case 200:
                        $scope.uiSelectList = result.data.data;
                        break;
                    default :
                        toaster.pop('error', "搜索失败", "搜索关键字代理区域下拉列表失败");
                        break;
                }
            });
        }
        
        // 选中代理区域后触发
        $scope.edChange = function () {
            agentService.isTheAreaAvailable($scope, $scope.agent.selected.id, id).then(function(result){
                switch (result.status) {
                    case 200:
                        switch (result.data.status){
                            case 1002:
                                toaster.pop('warning', "代理区域被占用", result.data.message);
                                $scope.agent.selected = undefined;
                                break;
                        }
                        break;
                    default :
                        toaster.pop('error', "检测区域可用失败", "检测所选的代理区域是否有代理商占用失败！");
                        $scope.agent.selected = undefined;
                        break;
                }
            });
        }

        $scope.AllowToAdd = true;
        $scope.edit = function(){
            if(!$scope.AllowToAdd){
                return;
            }
            $scope.AllowToAdd=false;

            if($scope.agent.selected == undefined) {
                toaster.pop('warning', "提示", "请选择代理区域");
                $scope.AllowToAdd = true;
                return;
            }

            switch ($scope.agent.gender) {
                case 0:
                case 1:
                    break;
                default:
                    toaster.pop('warning', "提示", "请选择性别");
                    $scope.AllowToAdd = true;
                    return;
            }

            agentService.update($scope).then(function(result){
                switch (result.status) {
                    case 200:
                        switch (result.data.status){
                            case 1002:
                                toaster.pop('warning', "代理区域被占用", result.data.message);
                                $scope.agent.selected = undefined;
                                break;
                            default :
                                $scope.query();
                                $scope.cancel();
                                break;
                        }
                        break;
                    default :
                        toaster.pop('warning', "修改失败", result.message);
                        break;
                }
                $scope.AllowToAdd=true;
            });
        }

        $scope.cancel = function () {
            $modalInstance.dismiss('close');
        };
}]);